<?php
namespace Model;

class SearchHandler {

	private $database = NULL;
	const OBJECT_EMPTY = 0;
	const OBJECT_SUCCESS = 1;
	const OBJECT_FAILED = 2;
	const OBJECT_SAVED = 3;

	public function __construct($a_database) {
		$this -> database = $a_database;
	}

	//Visar alla object
	public function getAllHits($searchWord, $catID) {
		
		if($catID > 1)
		{
			$sql = "SELECT o.*, c.CategoryName
				FROM object o 
				INNER JOIN category c 
				ON c.Cat_Id = o.Cat_Id
				ORDER BY Obj_Id DESC";
		}
		else
		{				
			$sql = "SELECT o.*, c.CategoryName
					FROM object o 
					INNER JOIN category c 
					ON c.Cat_Id = o.Cat_Id
					ORDER BY Obj_Id DESC";
		}
		$stmt = $this -> database -> SqlStatement($sql);
		$stmt -> execute();
		if ($stmt -> bind_result($objID, $userId, $catId, $title, $desc, $price, $date, $catName) == FALSE) {
			throw new \Exception($this -> m_mysqli -> error);
		}
		
		//lägger dom i en array
		$ret = new ObjectArray();
		while ($stmt -> fetch()) {
			$object = new Object($objID, $userId, $catId, $catName, $title, $desc, $price, $date);
			$ret -> add($object);
		}

		$stmt -> close();
		return $ret;
	}
}